const { server } = require("../config/config");
const { isUnEmptyObj } = require("../src/utils");
const chalk = require("chalk");
const moment = require("moment");

/**
 * 打印策略的补充
 * @param {*} app 
 */
function serverLogger(req, res, next) {
  const green = (text) => chalk.bold.green(text);
  const log = console.log;

  const serverName = chalk.bold.gray(`[${server.name}]：`);
  // 当前分两种一种为解密，一种为加密，当前使用解密
  // const url = chalk.bold(req.url);
  const url = chalk.bold(decodeURIComponent(req.url))
  let reqData = {};

  log(
    serverName,
    moment().format("YYYY-MM-DD HH:mm:ss"),
    `请求触发：${green(req.method)} ${url}`
  );

  if (isUnEmptyObj(req.body)) reqData = req.body;
  else if(isUnEmptyObj(req.query)) reqData = req.query;
  else if(isUnEmptyObj(req.params)) reqData = req.params;

  log(
    serverName,
    moment().format("YYYY-MM-DD HH:mm:ss"),
    `请求参数：${JSON.stringify(reqData)}`
  );
  
  next();
}

function logger(text) {
  const serverName = chalk.bold.gray(`[${server.name}]：`);
  const nowText = chalk.bold(text);
  const log = console.log;
  const timeStr = moment().format("YYYY-MM-DD HH:mm:ss");
  log(serverName, `${timeStr} - ${nowText}`)
}


module.exports = {
  serverLogger,
  logger
};